package com.antoco.operation.bigscreen.service.autogeneration.impl;

import com.antoco.operation.bigscreen.mapper.SensorWarnShowMapper;
import com.antoco.operation.bigscreen.model.dto.SensorWarnShowDTO;
import com.antoco.operation.bigscreen.model.entity.Sensor;
import com.antoco.operation.bigscreen.model.entity.SensorWarn;
import com.antoco.operation.bigscreen.repository.SensorWarnRepository;
import com.antoco.operation.bigscreen.service.WebSocketServer;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorWarnService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class SensorWarnServiceImpl extends AbstractModelServiceImpl implements SensorWarnService {

    @Autowired
    private SensorWarnRepository repository;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private SensorWarnShowMapper sensorWarnShowMapper;

    @Autowired
    private SensorServiceImpl sensorServiceImpl;

    @Override
    public SensorWarn save(SensorWarn entity) {
        repository.save(entity);
        try {
            Sensor sensor = sensorServiceImpl.findById(entity.getSensor().getId()).orElse(null);
            List<SensorWarnShowDTO> list = sensorWarnShowMapper.asDTOList(queryListByCondition(sensor.getDevBigType()));
            String channelName = "";
            if ((sensor.getDevBigType()).equals("1")) {
                channelName = "WATER_QUALITY";
            } else {
                channelName = "WATER_AMOUNT";
            }
            JSONArray jsonArray = JSONArray.fromObject(list);
            JSONObject json = new JSONObject();
            json.put("sensor_warn", jsonArray);
            JSONObject result = new JSONObject();
            result.put("data", json);
            result.put("channel", channelName);
            webSocketServer.send2Channel(channelName, result.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return entity;
    }

    @Override
    public List<SensorWarn> save(List<SensorWarn> entities) {
        return (List<SensorWarn>) repository.saveAll(entities);
    }

    @Override
    public void deleteById(String id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<SensorWarn> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public List<SensorWarn> findAll() {
        return (List<SensorWarn>) repository.findAll(Sort.by(Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<SensorWarn> findAll(Pageable pageable) {
        Page<SensorWarn> entityPage = repository.findAll(pageable);
        List<SensorWarn> entities = entityPage.getContent();
        return new PageImpl<>(entities, pageable, entityPage.getTotalElements());
    }

    @Override
    public SensorWarn update(SensorWarn entity, String id) {
        Optional<SensorWarn> optional = findById(id);
        if (optional.isPresent()) {
            return save(entity);
        }
        return null;
    }

    /**
     * 根据Sensor表的类型查询对应的传感器告警记录
     * @param type
     * @return
     */
    public List<SensorWarn> queryListByCondition(String type) {
        return repository.findBySensorType(type);
    }

    public List<SensorWarn> queryListBySensorId(String sensorId){
        return repository.findBySensorId(sensorId);
    }
}